$(function() {
	//获取商品id
	var localUrl = window.location.href;
	var localData = localUrl.split("=");
	var localText = decodeURI(localData[1]);
	if (localText == '' || localText == 'undefined') {
		var target = encodeURI("./liebiao.html");
		location.href = target;
	}

	//退出登录
	$("#login_but").on("click", "#logout", function() {
		$.ajax({
			type: "GET",
			url: "http://127.0.0.1:8080/user/logout",
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					window.location.href = './index.html';
				}
			}
		});
	})

	//加入购物车
	$("#addCar").click(function() {
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/goodsCar/addGoodsCar",
			dataType: "json",
			data: {
				"goodsId": localText
			},
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				layer.msg(data.msg);
			}
		})
	})

	//立即选购
	$("#buy").click(function() {
		if (localText == '') {
			layer.msg("数据异常");
		} else {
			var target = encodeURI("./order.html?id=" + localText);
			location.href = target;
		}
	});

	//获取登陆用户session
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/user/getSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				var node = $("#login_but");
				node.empty();
				var html = '<li style="color:#ffffff">' + data.data.userName + '，欢迎回来</li>';
				html += '<li>|</li><li><a href="javascript:void(0)" id="logout">退出</a></li>';
				node.html(html);
			}
		}
	});

	//获取商品信息
	$.ajax({
		type: "GET",
		url: "http://127.0.0.1:8080/goods/getGoods",
		dataType: "json",
		data: {
			"id": localText
		},
		success: function(data) {
			console.log(data)
			var goods = data.data.goods;
			$("#img_url").attr("src", goods.imgUrl);
			$("#goods_name").text(goods.goodsName);
			$("#goods_description").text(goods.description);
			$("#goods_price").text(goods.price + "元");
			var specificationStr = "";
			var specification = goods.specification;
			if (specification == 1) {
				specificationStr = "全网通版 6GB+64GB";
			} else if (specification == 2) {
				specificationStr = "全网通版 6GB+128GB";
			}
			$("#goods_specification").text(specificationStr);
			var natureStr = "";
			var nature = goods.nature;
			if (nature == 1) {
				natureStr = "黑亮色";
			} else if (nature == 2) {
				natureStr = "魅力蓝";
			} else if (nature == 3) {
				natureStr = "活力红";
			}
			$("#goods_nature").text(natureStr);
			$("#goods_info").text(goods.goodsName + " " + specificationStr + " " + natureStr);
			$("#goods_price_footer").text("￥" + goods.price);
			$("#goods_real_price").text("最终成交价：￥" + goods.price);
			var commentList = data.data.comment;
			var html = "";
			commentList.forEach(function(comment) {
				console.log(comment)
				html += '<li><span>' + comment.user_name +
					'&nbsp;&nbsp;&nbsp;&nbsp;<span>评分：' + comment.grade + '<br/></span></span><span>' +
					comment.comment + '</span></li><hr/>';
			});
			$("#comment_con").append(html);
		}
	});
})
